package com.ogawa.a7517.fragment;

import android.animation.ValueAnimator;
import android.view.View;
import android.widget.ImageView;

import com.ogawa.a7517.Constant;
import com.ogawa.a7517.R;
import com.ogawa.a7517.bean.DeviceStateBean;
import com.ogawa.a7517.bean.MassageArmchair;

public class ShoulderCheckFragment extends BaseFragment {
    private ImageView ivPoint1, ivPoint2, ivPoint3, ivPoint4, ivPoint5, ivPoint6;
    private ImageView ivUp, ivDown;
    private ValueAnimator animator;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_shoulder_check;
    }

    @Override
    public void initView(View view) {
        ivPoint1 = view.findViewById(R.id.iv_point_1);
        ivPoint2 = view.findViewById(R.id.iv_point_2);
        ivPoint3 = view.findViewById(R.id.iv_point_3);
        ivPoint4 = view.findViewById(R.id.iv_point_4);
        ivPoint5 = view.findViewById(R.id.iv_point_5);
        ivPoint6 = view.findViewById(R.id.iv_point_6);
        animator = ValueAnimator.ofInt(0, 6);
        animator.addUpdateListener(animation -> {
            int animatedValue = (int) animation.getAnimatedValue();
            setPointSelect(animatedValue);
        });
        animator.setDuration(3000);
        animator.setRepeatCount(-1);
        ivUp = view.findViewById(R.id.iv_up);
        ivUp.setOnClickListener(view1 ->
                activity7517.publish(Constant.Y_STATUS, "1"));
        ivDown = view.findViewById(R.id.iv_down);
        ivDown.setOnClickListener(view1 ->
                activity7517.publish(Constant.Y_STATUS, "2"));
        view.findViewById(R.id.iv_sure).setOnClickListener(view1 ->
                activity7517.publish(Constant.SHOULDER_DETECT_CHECK, "1"));
    }

    private void setPointSelect(int pos) {
        ivPoint1.setSelected(pos == 0);
        ivPoint2.setSelected(pos == 1);
        ivPoint3.setSelected(pos == 2);
        ivPoint4.setSelected(pos == 3);
        ivPoint5.setSelected(pos == 4);
        ivPoint6.setSelected(pos == 5);
    }

    @Override
    public void onStart() {
        super.onStart();
        setTitle(getString(R.string.shoulder_check));
        animator.start();
    }

    @Override
    public void onStop() {
        super.onStop();
        animator.pause();
    }

    @Override
    public void deviceStateChange() {
        super.deviceStateChange();
        DeviceStateBean stateBean = MassageArmchair.getInstance().getDeviceStatusBean();
        if (stateBean!=null){
            if (stateBean.getFunctions().getYStatus()!=null){
                int yStatus=Integer.parseInt(stateBean.getFunctions().getYStatus().getModelValue());
                ivUp.setSelected(yStatus==1);
                ivDown.setSelected(yStatus==2);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        animator.end();
    }
}
